package com.cyl.manager.yee.service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.cyl.manager.yee.mapper.ComplaintInfoFlatMapper;
import com.cyl.manager.ums.domain.entity.Member;
import com.cyl.manager.ums.service.MemberService;
import com.cyl.manager.yee.domain.ComplaintInfoFlat;
import com.cyl.manager.yee.pojo.query.ComplaintInfoFlatQuery;
import com.cyl.yeepay.dto.YeeComplaintInfoFlatDto;
import com.cyl.yeepay.setvice.YeePayService;

/**
 * 会员订单投诉Service业务层处理
 *
 *
 * @author hxw
 */
@Service
public class ComplaintInfoFlatService {
    @Autowired
    private ComplaintInfoFlatMapper complaintInfoFlatMapper;
    @Autowired
    private  YeePayService yeePayService;
    
    @Autowired
    private  MemberService memberService;
    /**
     * 查询会员订单投诉
     *
     * @param id 会员订单投诉主键
     * @return 会员订单投诉
     */
    public ComplaintInfoFlat selectById(Long id) {
        return complaintInfoFlatMapper.selectById(id);
    }

    
    
    
    /**
     * 查询会员订单投诉列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 会员订单投诉
     */
    public List<ComplaintInfoFlat> selectList(ComplaintInfoFlatQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<ComplaintInfoFlat> qw = new QueryWrapper<>();
        String complaintNo = query.getComplaintNo();
        if (!StringUtils.isEmpty(complaintNo)) {
            qw.eq("complaint_no", complaintNo);
        }
        LocalDateTime[] complaintTimeRange = query.getComplaintTimeRange();
        if (complaintTimeRange != null && complaintTimeRange.length == 2) {
            LocalDateTime start = complaintTimeRange[0];
            if (start != null) {
                qw.ge("complaint_time", start);
            }
            LocalDateTime end = complaintTimeRange[0];
            if (end != null) {
                qw.le("complaint_time", end);
            }
        }
        LocalDateTime[] tradeTimeRange = query.getTradeTimeRange();
        if (tradeTimeRange != null && tradeTimeRange.length == 2) {
            LocalDateTime start = tradeTimeRange[0];
            if (start != null) {
                qw.ge("trade_time", start);
            }
            LocalDateTime end = tradeTimeRange[0];
            if (end != null) {
                qw.le("trade_time", end);
            }
        }
        String amount = query.getAmount();
        if (!StringUtils.isEmpty(amount)) {
            qw.eq("amount", amount);
        }
        String problemDescription = query.getProblemDescription();
        if (!StringUtils.isEmpty(problemDescription)) {
            qw.eq("problem_description", problemDescription);
        }
        String complaintDetail = query.getComplaintDetail();
        if (!StringUtils.isEmpty(complaintDetail)) {
            qw.eq("complaint_detail", complaintDetail);
        }
        String complaintCount = query.getComplaintCount();
        if (!StringUtils.isEmpty(complaintCount)) {
            qw.eq("complaint_count", complaintCount);
        }
        String wechatMerchantno = query.getWechatMerchantno();
        if (!StringUtils.isEmpty(wechatMerchantno)) {
            qw.eq("wechat_merchantno", wechatMerchantno);
        }
        String merchantNo = query.getMerchantNo();
        if (!StringUtils.isEmpty(merchantNo)) {
            qw.eq("merchant_no", merchantNo);
        }
        String wechatOrderNo = query.getWechatOrderNo();
        if (!StringUtils.isEmpty(wechatOrderNo)) {
            qw.eq("wechat_order_no", wechatOrderNo);
        }
        String merchantOrderNo = query.getMerchantOrderNo();
        if (!StringUtils.isEmpty(merchantOrderNo)) {
            qw.eq("merchant_order_no", merchantOrderNo);
        }
        String applyRefundAmount = query.getApplyRefundAmount();
        if (!StringUtils.isEmpty(applyRefundAmount)) {
            qw.eq("apply_refund_amount", applyRefundAmount);
        }
        String complaintState = query.getComplaintState();
        if (!StringUtils.isEmpty(complaintState)) {
            qw.eq("complaint_state", complaintState);
        }
        String complaintOpenId = query.getComplaintOpenId();
        if (!StringUtils.isEmpty(complaintOpenId)) {
            qw.eq("complaint_open_id", complaintOpenId);
        }
        Long deptZbjId = query.getDeptZbjId();
        if (deptZbjId != null) {
            qw.eq("dept_zbj_id", deptZbjId);
        }
        Long deptTzId = query.getDeptTzId();
        if (deptTzId != null) {
            qw.eq("dept_tz_id", deptTzId);
        }
        Long deptDpId = query.getDeptDpId();
        if (deptDpId != null) {
            qw.eq("dept_dp_id", deptDpId);
        }
        Long memberId = query.getMemberId();
        if (memberId != null) {
            qw.eq("member_id", memberId);
        }
        String nicknameLike = query.getNicknameLike();
        if (!StringUtils.isEmpty(nicknameLike)) {
            qw.like("nickname", nicknameLike);
        }
        String cljg = query.getCljg();
        if (!StringUtils.isEmpty(cljg)) {
            qw.eq("cljg", cljg);
        }
        String clzt = query.getClzt();
        if (!StringUtils.isEmpty(clzt)) {
            qw.eq("clzt", clzt);
        }
        LocalDateTime clTime = query.getClTime();
        if (clTime != null) {
            qw.eq("cl_time", clTime);
        }
        return complaintInfoFlatMapper.selectList(qw);
    }

    /**
     * 新增会员订单投诉
     *
     * @param complaintInfoFlat 会员订单投诉
     * @return 结果
     */
    public int insert(ComplaintInfoFlat complaintInfoFlat) {
        complaintInfoFlat.setCreateTime(LocalDateTime.now());
        return complaintInfoFlatMapper.insert(complaintInfoFlat);
    }
    
    
    public int loadYeeDate(String startTime, String endTime, String merchantNo,Integer page,Integer limit) {
    	if(page==null) {
    		page = 1;
    	}
    	if(limit==null) {
    		limit = 100;
    	}
    	int a = 0;
    	merchantNo = yeePayService.getBaseMerchantNo(merchantNo);
    	List<YeeComplaintInfoFlatDto>  list  = yeePayService.complaintWechatList(startTime, endTime, merchantNo, page, limit, null, null);
    	if(list!=null) {
    		for (YeeComplaintInfoFlatDto cif : list) {
    	        try {
    	        		QueryWrapper<ComplaintInfoFlat> qw = new QueryWrapper<>();
    	    	        qw.eq("complaint_no", cif.getComplaintNo());
    	    	        qw.eq("merchant_order_no", cif.getMerchantOrderNo());
    	    	        qw.eq("complaint_open_id", cif.getComplaintOpenId());
    	    	        qw.eq("complaint_count", cif.getComplaintCount());
    	        	   int count =   complaintInfoFlatMapper.selectCount(qw);
    	    	        if(count==0) {
    	    	        	ComplaintInfoFlat entity   =  new ComplaintInfoFlat(cif);
    	    	        	Member member = memberService.selectMemberByOid(cif.getComplaintOpenId());
    	    	        	if(member!=null) {
    	    	        		entity.setDeptDpId(member.getDeptDpId());
    	    	        		entity.setDeptTzId(member.getDeptTzId());
    	    	        		entity.setDeptZbjId(member.getDeptZbjId());
    	    	        		entity.setMemberId(member.getId());
    	    	        		entity.setNickname(member.getNickname());
    	    	        		entity.setCreateTime(LocalDateTime.now());
    	    	        	}
    	    	        	int num = complaintInfoFlatMapper.insert(entity);
    	    	        	a = a+num;
    	    	        }
				} catch (Exception e) {
					 
				}
    	     
    		}
    	}
		return a;
    }

    /**
     * 修改会员订单投诉
     *
     * @param complaintInfoFlat 会员订单投诉
     * @return 结果
     */
    public int update(ComplaintInfoFlat complaintInfoFlat) {
        return complaintInfoFlatMapper.updateById(complaintInfoFlat);
    }

    /**
     * 批量删除会员订单投诉
     *
     * @param ids 需要删除的会员订单投诉主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return complaintInfoFlatMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除会员订单投诉信息
     *
     * @param id 会员订单投诉主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return complaintInfoFlatMapper.updateDelFlagByIds(ids);
    }
}
